<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单工厂模式</title>
    <style>
        .red{
            color: #f00;
        }
    </style>
</head>
<body>
    <h3>简单工厂模式</h3>
    <div>调用一个方法，返回一个类的实例，比如 jquery</div>

    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>

</body>
<script>
    class JQuery{
        constructor (selector) {
            let elems = document.querySelectorAll(selector)
            this.length = elems.length
            for (let i = 0; i < this.length; i++) {
                this[i] = elems[i]
            }
        }
        html () {
            return this[0].innerHTML
        }
        addClass (cls) {
            for (let i = 0; i < this.length; i++) {
                this[i].className = cls
            }
        }
    }
    // $ 实现了一个简单的工厂函数
    var $ = function (selector) {
        return new JQuery(selector)
    }

    let html = $('li').html()
    let lenght = $('li').length

    console.log(html, length)
    console.log('hahahahah')
    $('li').addClass('red')

</script>
</html>